package com.yiwenup.leetcode.offer_ii;

import com.yiwenup.leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * https://leetcode-cn.com/problems/kTOapQ/
 * <p>
 * 执行用时：16 ms, 在所有 Java 提交中击败了99.48%的用户
 * 内存消耗：45.4 MB, 在所有 Java 提交中击败了5.04%的用户
 **/
public class No055 {

    private Queue<Integer> queue;

    public No055(TreeNode root) {
        queue = new LinkedList<>();
        inOrder(root);
    }

    public void inOrder(TreeNode node) {
        if (node == null) return;
        inOrder(node.left);
        queue.offer(node.val);
        inOrder(node.right);
    }

    public int next() {
        return hasNext() ? queue.poll() : -1;
    }

    public boolean hasNext() {
        return queue.peek() != null;
    }
}
